package cn.yunhe.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import cn.yunhe.model.Admin;

public class UserDao {
	Connection conn = null;
	PreparedStatement pstmt = null;
	Statement st=null;
	ResultSet rs = null;

	public Admin checkUser(String name) {
		Admin admin = null;
		try {
			conn = DaoFacotry.getConn();
			String sql = "select * from t_user where username= ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, name);
			rs = pstmt.executeQuery();

			if (rs.next()) {
				admin = new Admin();
				admin.setName(rs.getString("username"));
				admin.setPwd(rs.getString("userpass"));
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DaoFacotry.closeAll(conn, pstmt, rs);
		}
		return admin;
	}

	public Admin userLogin(String name, String pwd) {
		Admin admin = null;
		try {
			conn = DaoFacotry.getConn();
 			String sql = "select * from t_user where username = ? and userpass = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, name);
			pstmt.setString(2, pwd);

			rs = pstmt.executeQuery();
 
 			
			if (rs.next()) {
				admin = new Admin();
				admin.setName(rs.getString("username"));
				admin.setPwd(rs.getString("userpass"));
			}
			
 		} catch (Exception e) {
			e.printStackTrace();
		}
		return admin;
	}

}
